package com.fjwt.gz.coupon.wx;

import cn.hutool.json.JSONUtil;
import com.fjwt.gz.core.third.util.RequestBodyUtils;
import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RequestParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import jakarta.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

/**
 * 小邮付的支付公众号配置的卡券核销处理逻辑
 **/

@Service
@Slf4j
public class WxCouponWriteNotifyService  {

    private static String logPrefix = "微信卡券核销回调";


    /**
     * 解析参数
     **/
    public Map<String, Object> paraseParamters(HttpServletRequest request, NotificationConfig notificationConfig) throws IOException {
        log.info(logPrefix + "解析前参数进来的参数是：{}", JSONUtil.toJsonStr(request));
        // HTTP 头 Wechatpay-Signature。应答的微信支付签名。
        String Signature = request.getHeader("Wechatpay-Signature");
        // HTTP 头 Wechatpay-Serial。微信支付平台证书的序列号，验签必须使用序列号对应的微信支付平台证书。
        String Serial = request.getHeader("Wechatpay-Serial");
        // HTTP 头 Wechatpay-Nonce。签名中的随机数。
        String Nonce = request.getHeader("Wechatpay-Nonce");
        // HTTP 头 Wechatpay-Timestamp。签名中的时间戳。
        String Timestamp = request.getHeader("Wechatpay-Timestamp");
        // HTTP 头 Wechatpay-Signature-Type。签名类型。
        String SignatureType = request.getHeader("Wechatpay-Signature-Type");

        RequestParam requestParam = new RequestParam.Builder()
                .serialNumber(Serial)
                .nonce(Nonce)
                .signature(Signature)
                .timestamp(Timestamp)
                .body(getRequestBody())
                .build();

        NotificationParser paramters = new NotificationParser(notificationConfig);
        log.info(logPrefix + "解析后参数：{}", JSONUtil.toJsonStr(paramters));
        Map couponParamters = paramters.parse(requestParam, Map.class);
        log.info(logPrefix + "解析后参数转map：{}", JSONUtil.toJsonStr(couponParamters));
        return couponParamters;
    }

    /**
     * 获取requestBody
     */
    public static String getRequestBody() throws IOException {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) attributes;
        HttpServletRequest request = (HttpServletRequest) servletRequestAttributes.getRequest();
        BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
        //读取输入流的内容转换为String类型IOUtils必须引入org.apache.dubbo.common.utils.IOUtils;包
        String body = RequestBodyUtils.read(reader);
        return body;
    }
}
